package com.netx.data;
import java.util.Map;
import java.util.HashMap;
import java.lang.reflect.Constructor;


public class DataSinks {

	private static Map<String,Class<? extends DataSink>> _classes;

	static {
		_classes = new HashMap<String,Class<? extends DataSink>>();
	}
	
	public static void registerClass(String ID, Class<? extends DataSink> dataSinkClass) {
		if(ID == null) {
			throw new IllegalArgumentException("null class ID");
		}
		else if(dataSinkClass == null) {
			throw new IllegalArgumentException("null Class object");
		}
		else if(_classes.containsKey(ID)) {
			throw new InvalidDataSinkException("\""+ID+"\": class already registered");
		}
		else if(!DataSink.class.isAssignableFrom(dataSinkClass)) {
			throw new InvalidDataSinkException(dataSinkClass.getName()+": not a DataSink");
		}
		else {
			// check constructors:
			Constructor<?>[] ctr = dataSinkClass.getConstructors();
			for(int i=0; i<ctr.length; i++) {
				Class<?>[] params = ctr[i].getParameterTypes();
				for(int j=0; j<params.length; j++) {
					if(params[i] != String.class) {
						throw new InvalidDataSinkException("data-sink protocol violation: illegal parameter class: "+params[i].getName());
					}
				}
			}
			_classes.put(ID, dataSinkClass);
		}
	}

	// for DatabaseAnalyzer:
	static Class<? extends DataSink> getRegisteredClass(String ID) {
		return (Class<? extends DataSink>)_classes.get(ID);
	}
}
